// Constructs an income panel for couples with DV report 
global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"


// Need the matching for perp in order to perform the reverse causality results 
use  "W:\intimate\data\dvonly_all.dta", clear 
preserve 
	g suspect_events = 1
	collapse (sum) suspect_events, by(shnro)
	save  "W:\intimate\data\men_suspect", replace
	ren suspect_events spouse_suspect_events 
	ren shnro sphnro0
	save  "W:\intimate\data\female_suspect", replace
restore 

preserve 
	g victim_events = 1
	collapse (sum) victim_events, by(ao_shnro)
	ren ao_shnro shnro
	save  "W:\intimate\data\men_victim", replace
	ren victim_events spouse_victim_events 
	ren shnro sphnro0
	save  "W:\intimate\data\female_victim", replace
restore 

preserve 
	g couple_events = 1
	collapse (sum) couple_events, by(shnro ao_shnro)
	ren ao_shnro sphnro0
	save  "W:\intimate\data\men_sus_couple", replace
	ren couple_events spouse_couple_events 
	ren shnro tmp
	ren sphnro0 shnro
	ren tmp sphnro0
	save  "W:\intimate\data\female_sus_couple", replace
restore 

// Merge in whether DV or not 
use "W:\intimate\data\allmen_data_clean", clear
merge m:1 shnro using  "W:\intimate\data\men_suspect"
drop if _merge==2
drop _merge

merge m:1 sphnro0 using  "W:\intimate\data\female_suspect"
drop if _merge==2
drop _merge

merge m:1 shnro using  "W:\intimate\data\men_victim"
drop if _merge==2
drop _merge

merge m:1 sphnro0 using  "W:\intimate\data\female_victim"
drop if _merge==2
drop _merge

merge m:1 shnro sphnro0 using  "W:\intimate\data\men_sus_couple"
drop if _merge==2
drop _merge

merge m:1 shnro sphnro0 using  "W:\intimate\data\female_sus_couple"
drop if _merge==2
drop _merge

// drop female suspects in any relationship 
drop if missing(spouse_suspect_events)==0
drop spouse_suspect_events spouse_couple_events
ren couple_events dv_couple_events 
g dv_couple = 1 - missing(dv_couple_events)

// drop male victims in any relationship 
drop if missing(victim_events)==0
drop victim_events


replace suspect_events=0 if missing(suspect_events)
rename spouse_victim_events victim_events
replace victim_events=0 if missing(victim_events)
replace dv_couple_events=0 if missing(dv_couple_events)

save "W:\intimate\data\allmen_data_clean_withDV", replace 


* Add in language and kids for men 
forvalues y=2006/2016{
use "W:\intimate\data\allmen_data_clean_withDV", clear
keep if year_start_cohab==`y'
gen year=year_start_cohab

g man_id = shnro
g woman_id = sphnro0
drop shnro sphnro0

// Add in women children
g shnro = woman_id
local y_1 = `y'-1
merge m:1  shnro using  "$data_folder\fertility_`y_1'.dta", keepusing(nchild) keep(master match) nogen 
replace nchild=0 if nchild==. 
ren nchild spouse_pre_nchild

merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`y'.dta", keepusing(kieli) keep(master match) nogen 
ren kieli spouse_kieli

drop shnro 
g shnro = man_id 
merge m:1 shnro using "D:\ready-made\FLEED_TOTAL\2016\fleed_kokonais_`y'.dta", keepusing(kieli) keep(master match) nogen 

drop shnro 
ren man_id shnro
ren woman_id sphnro0

tempfile cat_`y'
save `cat_`y'', replace 
}

clear 
forvalues y=2006/2016{
    append using `cat_`y''
}

save "W:\intimate\data\allmen_data_clean_withDV", replace




// Matching for perp 
use "W:\intimate\data\allmen_data_clean_withDV", clear

// Cleaning 
* keep victims and "clean" others
g insample = 0
replace insample=1 if dv_couple==1
replace insample=1 if dv_couple==0 & victim_events==0 & suspect_events==0
keep if insample==1
drop insample

replace nchildB1=5 if nchildB1>=5

gen language=(kieli=="fi")
gen spouse_language=(spouse_kieli=="fi")

// merge crime
g year_event = year_start_cohab-1
merge 1:1 shnro year_event using "W:\intimate\data\cumul_crime_violent"
drop if _merge==2
replace cum_crimes = 0 if _merge==1
drop _merge sphnro
ren cum_crimes pre_crimes

g sphnro = sphnro0
merge 1:1 sphnro year_event using "W:\intimate\data\cumul_crime_violent", keepusing(cum_crimes)
drop if _merge==2
replace cum_crimes = 0 if _merge==1
drop _merge sphnro
ren cum_crimes spouse_pre_crimes

replace pre_crimes = 5 if pre_crimes>5
replace spouse_pre_crimes = 5 if spouse_pre_crimes>5


gen spouse_college=spouse_pre_educ>=6
gen spouse_high_school=spouse_pre_educ>=3 & spouse_pre_educ<=5
gen spouse_ho=spouse_pre_educ<3

gen college=educ>=6
gen high_school=educ>=3 & educ<=5
gen ho=educ<3



// split into groups - some merging of small groups
g data_group = .
replace data_group =1 if ptoim1B1 ==0 & nchildB1==0 & spouse_pre_ptoim1==0 & college==0
replace data_group =2 if ptoim1B1 ==0 & nchildB1==0 & spouse_pre_ptoim1==0 & college==1
replace data_group =3 if ptoim1B1 ==0 & nchildB1==0 & spouse_pre_ptoim1==1 & college==0
replace data_group =3 if ptoim1B1 ==0 & nchildB1==0 & spouse_pre_ptoim1==1 & college==1


replace data_group =4 if ptoim1B1 ==0 & nchildB1>0 & spouse_pre_ptoim1==0 & college==0
replace data_group =4 if ptoim1B1 ==0 & nchildB1>0 & spouse_pre_ptoim1==0 & college==1
replace data_group =5 if ptoim1B1 ==0 & nchildB1>0 & spouse_pre_ptoim1==1 & college==0
replace data_group =6 if ptoim1B1 ==0 & nchildB1>0 & spouse_pre_ptoim1==1 & college==1


replace data_group =7 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1==0 & spouse_pre_ptoim1==0 & college==0
replace data_group =8 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1==0 & spouse_pre_ptoim1==0 & college==1
replace data_group =9 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1==0 & spouse_pre_ptoim1==1 & college==0
replace data_group =9 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1==0 & spouse_pre_ptoim1==1 & college==1
replace data_group =10 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1>0 & spouse_pre_ptoim1==0 & college==0
replace data_group =10 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1>0 & spouse_pre_ptoim1==0 & college==1
replace data_group =11 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1>0 & spouse_pre_ptoim1==1 & college==0
replace data_group =11 if ptoim1B1 ==1 & tyotuB1 < 20000 & nchildB1>0 & spouse_pre_ptoim1==1 & college==1

replace data_group =12 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1==0 & spouse_pre_ptoim1==0 & college==0
replace data_group =13 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1==0 & spouse_pre_ptoim1==0 & college==1
replace data_group =14 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1==0 & spouse_pre_ptoim1==1 & college==0
replace data_group =15 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1==0 & spouse_pre_ptoim1==1 & college==1
replace data_group =16 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1>0 & spouse_pre_ptoim1==0 & college==0
replace data_group =17 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1>0 & spouse_pre_ptoim1==0 & college==1
replace data_group =18 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1>0 & spouse_pre_ptoim1==1 & college==0
replace data_group =19 if ptoim1B1 ==1 & tyotuB1 >= 20000 & nchildB1>0 & spouse_pre_ptoim1==1 & college==1


sort dv_couple data_group shnro year_start_cohab

forvalues i = 1/19 {
    preserve
	keep if data_group==`i'
	save "$data_folder\match_data_group`i'", replace
	restore
}

forvalues i = 1/19 {


	use "$data_folder\match_data_group`i'", clear
	
	set seed 12345
	sort dv_couple data_group shnro year_start_cohab
	
	forvalues s = 1/4 {
		xtile inc_group`s' = tyotuB`s', nq(5)
		g cohabit`s' = 1 - missing(sphnroB`s')
	}
	g age_group = floor(age/5)
	

	g spouse_missing = 0
	replace spouse_missing = 1 if missing(spouse_pre_age)
	xtile spouse_group1 = spouse_pre_tyotu, nq(5)
	replace spouse_group1 = 0 if spouse_missing==1
	g spouse_age = floor(spouse_pre_age/5)
	replace spouse_age = 0 if spouse_missing==1
	replace spouse_pre_ptoim1 = 0 if spouse_missing==1
	
	

	logit dv_couple i.age_group ho ///
				i.inc_group1 i.ptoim1B1 ///
				i.inc_group2 i.ptoim1B2 ///
				i.inc_group3 i.ptoim1B3 ///
				i.spouse_age spouse_college spouse_ho i.spouse_group1 i.nchildB1 ///
				i.spouse_missing i.year_start_cohab ///
				cohabit* ///
				i.inc_group4 i.ptoim1B4 ///
				i.pre_crimes i.spouse_pre_crimes 
				
				
	predict dv_prob, pr
	g missing_prob = missing(dv_prob)
	table missing_prob dv_couple
	drop if missing_prob==1
	

	
	// find 5 nearest neighbours 
	sort dv_couple data_group shnro year_start_cohab
	gen match_n = _n
	gen rand_n = runiform()
	
	sort match_n
	teffects nnmatch (rand_n dv_prob) (dv_couple), ///
			gen(match_ob) nneighbor(5)
			
	preserve
		keep if dv_couple==1 
		keep match_n match_ob1 match_ob2 match_ob3 match_ob4 match_ob5
		tempfile idwide
		save `idwide'
		
		forvalues n = 1/5 {
			use `idwide', clear
			keep match_ob`n' match_n
			ren match_n match_id
			ren match_ob`n' match_n
			tempfile match`n'
			save `match`n''
		}
		use `idwide'
		keep match_n
		g match_id = match_n
		tempfile match0
		save `match0'
		
		use `match0', clear
		forvalues n = 1/5 {
			append using `match`n''
		}
		drop if missing(match_n)
		tempfile mergeids
		save `mergeids'
					
	restore
	
	merge 1:m match_n using `mergeids'
	
	keep if _merge==3
	drop _merge 
	keep shnro year_start_cohab match_id
	
	merge m:1 shnro year_start_cohab using "$data_folder\match_data_group`i'.dta"
	keep if _merge == 3
	drop _merge
	sort dv_couple data_group shnro year_start_cohab match_id
	save "$data_folder\match_complete_group`i'", replace
}


use "$data_folder\match_complete_group1", clear
forvalues i = 2/19 {
    append using "$data_folder\match_complete_group`i'"
}
tab dv_couple
sort shnro year_start_cohab match_id
save "W:\intimate\data\match_perp_data_clean_new", replace
*/ 




use  "W:\intimate\data\match_perp_data_clean_new", clear
drop spouse* data_group

merge m:1 shnro year_start_cohab using "W:\intimate\data\allcouple_data_clean_addfirm", keepusing (employee* earnings* newhire* plant_closed_year* switch*)
drop if _merge==2
drop _merge 

gen turnover_rateB5=-switchB5/employeeB5
gen turnover_rateB4=-switchB4/employeeB4
gen turnover_rateB3=-switchB3/employeeB3
gen turnover_rateB2=-switchB2/employeeB2
gen turnover_rateB1=-switchB1/employeeB1
gen turnover_rate0=-switch0/employeeB1
gen turnover_rateF1=-switchF1/employeeB1
gen turnover_rateF2=-switchF2/employeeB1
gen turnover_rateF3=-switchF3/employeeB1
gen turnover_rateF4=-switchF4/employeeB1
gen turnover_rateF5=-switchF5/employeeB1


// change into long format
keep shnro year_start_cohab dv_couple employee* earnings* newhire* plant_closed_year* turnover_rate* match_id ///
			age seutukunta16* educ ptoim1*
local varlist  employee earnings newhire plant_closed_year turnover_rate

replace plant_closed_yearB5=plant_closed_yearB1 if plant_closed_yearB5==.
replace plant_closed_yearB4=plant_closed_yearB1 if plant_closed_yearB4==.
replace plant_closed_yearB3=plant_closed_yearB1 if plant_closed_yearB3==.
replace plant_closed_yearB2=plant_closed_yearB1 if plant_closed_yearB2==.
replace plant_closed_yearB1=plant_closed_yearB1 if plant_closed_yearB1==.
replace plant_closed_year0=plant_closed_yearB1 if plant_closed_year0==.
replace plant_closed_yearF1=plant_closed_yearB1 if plant_closed_yearF1==.
replace plant_closed_yearF2=plant_closed_yearB1 if plant_closed_yearF2==.
replace plant_closed_yearF3=plant_closed_yearB1 if plant_closed_yearF3==.
replace plant_closed_yearF4=plant_closed_yearB1 if plant_closed_yearF4==.
replace plant_closed_yearF5=plant_closed_yearB1 if plant_closed_yearF5==.

local varlist  employee earnings newhire plant_closed_year turnover_rate seutukunta16 ptoim1

forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}


foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

gen group_id = _n

local varlist  employee earnings newhire plant_closed_year turnover_rate seutukunta16 ptoim1
reshape long `varlist', i(group_id) j(time)

replace turnover_rate=. if missing(turnover_rate)==0 & turnover_rate>2
rename employee firm_size
replace firm_size=10000 if firm_size>10000 & firm_size~=.
replace turnover_rate=0 if turnover_rate<0

gen plant_close=(year>=plant_closed_year)

// Add in Outside Options

g male=1
g time_ = time-6
g year = year_start_cohab + (time - 6)

rename educ level 
gen educ=1 if level<3
replace educ=2 if leve>=3 & level<=5
replace educ=3 if level>=6

rename age age_temp
gen age = floor(age_temp/5)

merge m:1 year educ seutukunta16 age male using "W:\intimate\data\munic_edu_age_gender_oo", keepusing(emp obs)
drop if _merge==2 
drop _merge 

gen emp_rate_lo=(emp-ptoim1)/(obs-1)
cap drop obs emp 

// DiD

*Define dummy used in the event studies
gen treat= dv_couple==1


*Time displacement dummies
g dpl_5=time_==-5 & treat==1
g dpl_4=time_==-4 & treat==1
g dpl_3=time_==-3 & treat==1
g dpl_2=time_==-2 & treat==1
g dpl_1=time_==-1 & treat==1
g dpl_0=time_==0 & treat==1
gen dpl1=time_==1 & treat==1
gen dpl2=time_==2 & treat==1
gen dpl3=time_==3 & treat==1
gen dpl4=time_==4 & treat==1
gen dpl5=time_==5 & treat==1

gen treatPost= treat==1 & time_>=0
egen full_fe = group(match_id time)
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe = "full_fe year_start_cohab" 
global cluster = "match_id"

est clear 

foreach thing in earnings firm_size turnover_rate emp_rate_lo {
    reghdfe `thing'  treatPost treat,  absorb($fe)  cluster($cluster)
	est sto robust_`thing'
	su `thing' if treat==1 & time_<0
	local pmean = round(r(mean), 0.001)
	estadd local premean "`pmean'"
}

esttab robust_* using "$result_folder\perp_external_shock.tex", replace b(4) se(4) r2(4) ///
			keep(treatPost treat) scalars("premean Dependent Mean")


